#include "TeleporterInfoMgr.h"
#include "Character/monster/MonsterRefCfg.h"
#include "Common/TableData/TeleportTeleportCfg.h"


TeleporterInfoMgr::TeleporterInfoMgr()
{

}

TeleporterInfoMgr::~TeleporterInfoMgr()
{

}

bool TeleporterInfoMgr::Init()
{
	std::vector<uint32_t> vec;
	vec.clear();
	g_GetMapLocationCfg()->getAllTeleporterId(vec);

	for (size_t i = 0; i < vec.size(); i++)
	{
		uint32_t id = vec[i];
		TeleporterLocation* pLoc = g_GetMapLocationCfg()->getTeleporterLocation(id);
		if (nullptr == pLoc)
		{
			LogErrFmtPrint("TeleporterInfoMgr::Init....nullptr == pLoc...id:%u ", id);
			return false;
		}
		if (nullptr != GetTeleporterInfo(id))
		{
			LogErrFmtPrint("TeleporterInfoMgr::Init....repeated teleprot...id:%u ", id);
			return false;
		}

		TeleporterInfo stTeleportInfo(id, pLoc->m_pos);
		if (!stTeleportInfo.Init())
		{
			LogErrFmtPrint("TeleporterInfoMgr::Init....stTeleportInfo.Init failed...teleporterId:%u ", id);
			return false;
		}
		m_mapTeleInfo[stTeleportInfo.GetConfigId()] = stTeleportInfo;
	}

	return true;
}

TeleporterInfo* TeleporterInfoMgr::GetTeleporterInfo(uint32_t teleporterId)
{
	mapTeleIter iter = m_mapTeleInfo.find(teleporterId);
	if (iter != m_mapTeleInfo.end())
	{
		return &iter->second;
	}
	return nullptr;
}

bool TeleporterInfoMgr::UnInit()
{
	mapTeleIter iter = m_mapTeleInfo.begin();
	for (; iter != m_mapTeleInfo.end(); iter++)
	{
		if (!iter->second.UnInit())
		{
			LogErrFmtPrint("teleporter info id:%d UnInit failed", iter->first);
		}
	}

	m_mapTeleInfo.clear();
	return true;
}
